<template>
	<section>
		<!--工具条-->
		<el-col  v-if='isShow'  :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">

				<el-form-item label="运营商" prop="operatoruid">
					<el-select v-model="filters.operatoruid" clearable filterable placeholder="请选择运营商">
						<el-option value="0" label="全部">全部</el-option>
						<el-option
								v-for="item in operator"
								:key="item.operatoruid"
								:label="item.operatorname"
								:value="item.operatoruid">
						</el-option>
					</el-select>
				</el-form-item>

				<el-form-item label="供货商" prop="useruuid" >
					<el-select v-model="filters.useruuid" clearable filterable placeholder="请选择供货商" >
						<el-option value="0" label="全部">全部</el-option>
						<el-option
								v-for="item in users"
								:key="item.user.useruuid"
								:label="item.user.username"
								:value="item.user.useruuid">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="省份" prop="provinceuuid">
					<el-select v-model="filters.provinceuuid" clearable filterable placeholder="请选择省份">
						<el-option value="0" label="全部">全部</el-option>
						<el-option
								v-for="item in info"
								:key="item.provinceuuid"
								:label="item.provincename"
								:value="item.provinceuuid">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="业务类型" prop="busuuid"  >
					<el-select v-model="filters.busuuid" clearable filterable placeholder="请选择业务类型">
						<el-option value="0" label="全部">全部</el-option>
						<el-option
								v-for="item in bus"
								:key="item.busuuid"
								:label="item.busname"
								:value="item.busuuid">
						</el-option>
					</el-select>
				</el-form-item>

				<!--<el-form-item>-->
					<!--<el-input v-model="filters.facevalue" placeholder="面值"></el-input>-->
				<!--</el-form-item>-->
<!--				<el-form-item label="供货商" prop="useruuid">-->
<!--					<el-select v-model="filters.useruuid" placeholder="请选择供货商"  >-->
<!--						<el-option value="0" label="全部">全部</el-option>-->
<!--						<el-option-->
<!--								v-for="item in users"-->
<!--								:key="item.user.useruuid"-->
<!--								:label="item.user.username"-->
<!--								:value="item.user.useruuid">-->
<!--						</el-option>-->
<!--					</el-select>-->
<!--				</el-form-item>-->
                <el-form-item>
                    <el-date-picker type="datetime" placeholder="开始时间"  format="yyyy-MM-dd HH:mm:ss"    v-model="filters.starttime" style="width: 100%;"></el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-date-picker type="datetime" placeholder="结束时间" format="yyyy-MM-dd HH:mm:ss"   v-model="filters.endtime" style="width: 100%;"></el-date-picker>
                </el-form-item>
				<el-form-item>
					<el-button type="primary" v-if='isShow'  v-on:click="countProfit">查询</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="profits" v-if='isShow'  highlight-current-row v-loading="listLoading" max-height="620" style="width: 100%;">
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="分销商"  sortable>
			</el-table-column>
			<el-table-column prop="product" label="产品名称"  sortable>
			</el-table-column>
			<el-table-column prop="bustype" label="业务类型"  sortable>
			</el-table-column>
			<el-table-column prop="num" label="充值笔数" sortable>
			</el-table-column>
			<el-table-column prop="price" label="售价" sortable>
			</el-table-column>
			<el-table-column prop="cb" label="成本" sortable>
			</el-table-column>
			<el-table-column prop="profit" label="利润" sortable>
			</el-table-column>
			<el-table-column prop="rate" label="利润率"  sortable>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-pagination layout="total,sizes,prev, pager, next"
					   @current-change="handleCurrentChange"
					   @size-change="handleSizeChange"
					   :total="total" >
		</el-pagination>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import {
		countProfitRate,
		findAllDownShopInfo,
		findAllUpShopInfo,
		findOpeProBusShop,
		getOperatorListPage
	} from '../../api/api';
    import moment from 'moment';

	export default {
		data() {
			return {
				filters: {
					facevalue: '',
                    useruuid:'',
                    busuuid:'',
                    provinceuuid:'',
					operatoruid:'',
					starttime:moment(new Date(),"YYYY-MM-DD HH:mm:ss").format('YYYY-MM-DD') + " 00:00:00",
					endtime:moment(new Date(),"YYYY-MM-DD HH:mm:ss").format('YYYY-MM-DD') + " 23:59:59"
				},
				users: [],
                bus: [],
                info:[],
				total: 0,
                profits:[],
				operator:[],
                start: 0,
                pageSize: 10,
				listLoading: false,
				isShow: JSON.parse(sessionStorage.getItem("user")).usertype === 2 ? true : false,
			}
		},
		methods: {
			getOperatorListPage() {
				let para = {
					start: 1,
					pageSize:10,
					operatorname:'',
				};
				this.listLoading = true;
				getOperatorListPage(para).then((datas) => {
					let { message, code, data } = datas;
					if (code !== "0") {
						this.$message({
							message: message,
							type: 'error'
						});
					} else if(data && data.result){
						this.operator = data.result;
					}
				});
			},
			findAllUpShopInfo:function () {
				let para = {
					page: this.page,
				};
				findAllUpShopInfo(para).then(datas => {
					let {message, code, data} = datas;
					if (code !== "0") {
						if (code === "37") {
							this.$router.push({path: '/login'});
						}
						this.$message({
							message: message,
							type: 'error'
						});
					} else {
						this.users = data;
					}
				}).catch(e =>{
					this.listLoading = false;
				});;
			},
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			}, handleCurrentChange: function (val) {
                this.start = val -1;
                this.countProfit();
            },
            handleSizeChange: function (val) {
                this.pageSize = val;
                this.countProfit();
            },
            countProfit() {
				let para = {
                    pageSize: this.pageSize,
                    start: this.start,
                    facevalue: this.filters.facevalue,
                    useruuid:this.filters.useruuid == "0" ? "": this.filters.useruuid, //JSON.parse(sessionStorage.getItem("user")).useruuid,
                    busuuid:this.filters.busuuid == "0" ? "" : this.filters.busuuid,
					operatoruid:this.filters.operatoruid == "0" ? "" : this.filters.operatoruid,
                    provinceuuid:this.filters.provinceuuid == "0" ? "" : this.filters.provinceuuid,
                    starttime: this.filters.starttime == ""  || this.filters.starttime == undefined ? "" : moment(this.filters.starttime,"YYYY-MM-DD HH:mm:ss").format('YYYY-MM-DD HH:mm:ss'),
                    endtime: this.filters.endtime == "" || this.filters.endtime == undefined ? "" : moment(this.filters.endtime,"YYYY-MM-DD HH:mm:ss").format('YYYY-MM-DD HH:mm:ss'),
				};
				this.listLoading = true;
				countProfitRate(para).then(datas => {
                        let {message, code, data} = datas;
                        if (code !== "0") {
                            this.$message({
                                message: message,
                                type: 'error'
                            });
                        } else {
                            this.profits = data.result;
                            this.total = data.totalCount;
                        }
                        this.listLoading = false;
                });
			},findAllDownShopInfo: function () {
                let para = {useruuid: JSON.parse(sessionStorage.getItem("user")).useruuid};
                findAllDownShopInfo(para).then(datas => {
                    let {message, code, data} = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.users = data;
                    }
                });
            },findOpeProBusShop(){
                let para = {
                    test: ''
                };
                findOpeProBusShop(para).then(datas => {
                    let {message, code, data} = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.info = data.info;
                        this.bus = data.bus;
                    }
					this.countProfit();
                });
			}
		},
		mounted() {
			this.findAllUpShopInfo();
			//this.findAllDownShopInfo();
		    this.findOpeProBusShop();
		    this.getOperatorListPage();
		}
	}

</script>

<style scoped>

</style>
